package com.example.star.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.star.R;
import com.example.star.adapter.StarAdapter;
import com.example.star.api_service.ApiStar;
import com.example.star.entity.Star;
import com.example.star.json.JsonParser;
import com.example.star.retrofit.ApiClient;
import com.example.star.view.activity.StarDetailActivity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Tab1Fragment extends Fragment {

    private StarAdapter adapter;
    private List<Star> localSingers;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tab1, container, false); // 这里假设布局使用 fragment_tab1 的布局
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ListView listView = view.findViewById(R.id.list_view_stars);

        // 先从本地 JSON 文件获取歌手数据
        localSingers = new ArrayList<>();
        List<Star> allStarsFromJson = JsonParser.parseStarsFromJson(requireContext(), "stars_singer.json");
        if (allStarsFromJson != null) {
            for (Star star : allStarsFromJson) {
                if ("歌手".equals(star.getProfession())) {
                    localSingers.add(star);
                }
            }
        }

        ApiStar apiStar = ApiClient.getApiStar();
        Call<List<Star>> call = apiStar.getStars();
        call.enqueue(new Callback<List<Star>>() {
            @Override
            public void onResponse(Call<List<Star>> call, Response<List<Star>> response) {
                if (response.isSuccessful()) {
                    List<Star> allStarsFromNetwork = response.body();
                    List<Star> networkSingers = new ArrayList<>();
                    if (allStarsFromNetwork != null) {
                        for (Star star : allStarsFromNetwork) {
                            if ("歌手".equals(star.getProfession())) {
                                networkSingers.add(star);
                            }
                        }
                    }

                    localSingers.addAll(networkSingers);

                    adapter = new StarAdapter(requireContext(), R.layout.list_item_star, localSingers);
                    listView.setAdapter(adapter);

                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Star star = localSingers.get(position);
                            Intent intent = new Intent(requireContext(), StarDetailActivity.class);
                            intent.putExtra("imageResource", star.getImageResource());
                            intent.putExtra("starName", star.getName());
                            intent.putExtra("age", star.getAge());
                            intent.putExtra("birthDate", star.getBirthDate());
                            intent.putExtra("birthPlace", star.getBirthPlace());
                            intent.putExtra("nationality", star.getNationality());
                            intent.putExtra("actingExperience", star.getActingExperience());
                            intent.putExtra("representativeWorks", star.getRepresentativeWorks());
                            intent.putExtra("profession", star.getProfession());
                            intent.putExtra(StarDetailActivity.EXTRA_STAR, star);
                            startActivity(intent);
                        }
                    });
                }
            }

            @Override
            public void onFailure(Call<List<Star>> call, Throwable t) {
                t.printStackTrace();
                // 网络请求失败，只使用本地数据
                adapter = new StarAdapter(requireContext(), R.layout.list_item_star, localSingers);
                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Star selectedStar = localSingers.get(position);
                        Intent intent = new Intent(requireContext(), StarDetailActivity.class);
                        intent.putExtra(StarDetailActivity.EXTRA_STAR, selectedStar);
                        startActivity(intent);
                    }
                });
            }
        });
    }

    public void filter(String keyword) {
        if (adapter != null) {
            adapter.filter(keyword);
        }
    }
}